package com.joker.web.controller.orange;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.joker.common.constant.CacheConstants;
import com.joker.common.core.domain.entity.SysUser;
import com.joker.common.utils.redis.RedisUtils;
import com.joker.orange.domain.*;
import com.joker.orange.mapper.*;
import com.joker.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 扣单逻辑
 */
@RequiredArgsConstructor
@Service
@Transactional
public class AppDeductionOrderService {

    private final UserInfoMapper userInfoMapper;
    private final SysUserMapper sysUserMapper;
    private final UserOrderRecordMapper userOrderRecordMapper;
    private final MemberOrderMapper orderMapper;
    private final UserDeductionMapper deductionMapper;
    private final AgentUserStatisticsMapper agentUserStatisticsMapper;

    /**
     * 扣单逻辑
     *
     * @param userId  用户ID
     * @param orderNo 订单编号
     */
    public void DeductionRegister(String userId, String orderNo) {

        MemberOrder order = orderMapper.selectOne(
            new LambdaUpdateWrapper<MemberOrder>().eq(
                MemberOrder::getOrderNo, orderNo
            ));

        UserInfo userInfo = userInfoMapper.selectById(userId);
        // 上级类型（0-用户、1-平台、2-代理）
        if ("2".equals(userInfo.getSuperiorType())) {
            // 查询出上级
            SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(userInfo.getSpreadId()));
            if (ObjectUtil.isNotNull(sysUser)) {
                // 查询代理的订单数量
                Integer orderCount = getOrderCount(sysUser.getUserId()) + 1;

                // 判断上级的代理等级 代理等级==1、2、3
                if ("1".equals(sysUser.getAgentLevel())) {

                    // 查询平台的扣单比例，判断平台是否扣单
                    UserDeduction deduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                        .eq(UserDeduction::getDeductionType, "0"));

                    // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空
                    if (ObjectUtil.equals(orderCount, deduction.getOrderCount())) {
                        RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), "0");
                        userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                            .set(UserOrderRecord::getOrderCount, "0")
                            .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
                        // todo 记录扣单
                    } else {
                        // 不满足扣单，按照分成比例进行分配
                        BigDecimal commissionRatio = sysUser.getCommissionRatio();
                        BigDecimal divideMoney = commissionRatio.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                        sysUser.setBalance(sysUser.getBalance().add(divideMoney));
                        sysUserMapper.updateById(sysUser);
                        // 更新订单数量
                        RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), orderCount);
                        userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                            .set(UserOrderRecord::getOrderCount, orderCount)
                            .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
                        // 记录代理统计
                        AgentUserStatistics(divideMoney, sysUser, userInfo.getAppType(), order.getOrderType());
                    }
                } else if ("2".equals(sysUser.getAgentLevel())) {
                    // 代理等级是2级，查询2级代理的上级
                    SysUser firstUser = sysUserMapper.selectVoOne(new LambdaUpdateWrapper<SysUser>()
                        .eq(SysUser::getSuperiorAgent, sysUser.getUserId()));
                    // 2级代理的上级是平台
                    if ("0".equals(firstUser.getAgentLevel())) {

                        // 查询平台的扣单比例，判断平台是否扣单
                        UserDeduction deduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                            .eq(UserDeduction::getDeductionType, "0"));

                        // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空

                        if (ObjectUtil.equals(orderCount, deduction.getOrderCount())) {
                            RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), "0");
                            userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                                .set(UserOrderRecord::getOrderCount, "0")
                                .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
                            // todo 记录扣单
                        } else {
                            // 不满足扣单，按照分成比例进行分配
                            BigDecimal commissionRatio = sysUser.getCommissionRatio();
                            BigDecimal divideMoney = commissionRatio.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                            sysUser.setBalance(sysUser.getBalance().add(divideMoney));
                            sysUserMapper.updateById(sysUser);
                            // 更新订单数量
                            RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), orderCount);
                            userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                                .set(UserOrderRecord::getOrderCount, orderCount)
                                .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
                            // 记录代理统计
                            AgentUserStatistics(divideMoney, sysUser, userInfo.getAppType(), order.getOrderType());
                        }
                        // 2级代理的上级是1级代理
                    } else if ("1".equals(firstUser.getAgentLevel())) {

                        // 查询平台的扣单比例，判断平台是否扣单
                        UserDeduction deduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                            .eq(UserDeduction::getDeductionType, "0"));

                        // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空
                        if (ObjectUtil.equals(orderCount, deduction.getOrderCount())) {
                            RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), orderCount);
                            userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                                .set(UserOrderRecord::getOrderCount, "0")
                                .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
                            // todo 记录扣单
                        } else {
                            // 平台不扣单，查询一级代理是否扣单
                            UserDeduction firstDeduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                                .eq(UserDeduction::getDeductionType, "1")
                                .eq(UserDeduction::getUserId, firstUser.getUserId()));
                            // 一级代理扣单
                            if (ObjectUtil.equals(orderCount, firstDeduction.getOrderCount())) {
                                // 一级代理扣单
                                AgentUserStatistics(order.getOrderAmount(), firstUser, userInfo.getAppType(), order.getOrderType());
                                firstUser.setBalance(firstUser.getBalance().add(order.getOrderAmount()));
                                sysUserMapper.updateById(firstUser);
                                // todo 记录扣单
                            } else {
                                // 不满足扣单，按照分成比例进行分配
                                // 2级代理先分配。然后是一级代理得比例减去2级代理得比例才是一级得到得
                                BigDecimal commissionRatio = sysUser.getCommissionRatio();
                                BigDecimal divideMoney = commissionRatio.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                                sysUser.setBalance(sysUser.getBalance().add(divideMoney));
                                sysUserMapper.updateById(sysUser);
                                AgentUserStatistics(divideMoney, sysUser, userInfo.getAppType(), order.getOrderType());

                                BigDecimal commissionRatio_1 = firstUser.getCommissionRatio().subtract(commissionRatio);
                                BigDecimal divideMoney_1 = commissionRatio_1.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                                firstUser.setBalance(sysUser.getBalance().add(divideMoney_1));
                                sysUserMapper.updateById(firstUser);
                                AgentUserStatistics(divideMoney_1, firstUser, userInfo.getAppType(), order.getOrderType());

                            }
                        }
                    }
                    // 三级代理
                } else if ("3".equals(sysUser.getAgentLevel())) {
                    // 查询3级代理的上级
                    SysUser superUser = sysUserMapper.selectVoOne(new LambdaUpdateWrapper<SysUser>()
                        .eq(SysUser::getSuperiorAgent, sysUser.getUserId()));
                    // 上级代理为平台
                    if ("0".equals(superUser.getAgentLevel())) {

                        // 查询平台的扣单比例，判断平台是否扣单
                        UserDeduction deduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                            .eq(UserDeduction::getDeductionType, "0"));

                        // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空
                        if (ObjectUtil.equals(orderCount, deduction.getOrderCount())) {
                            RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), "0");
                            userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                                .set(UserOrderRecord::getOrderCount, "0")
                                .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
                            // todo 记录扣单
                        } else {
                            // 不满足扣单，按照分成比例进行分配
                            BigDecimal commissionRatio = sysUser.getCommissionRatio();
                            BigDecimal divideMoney = commissionRatio.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                            sysUser.setBalance(sysUser.getBalance().add(divideMoney));
                            sysUserMapper.updateById(sysUser);
                            // 更新订单数量
                            RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), orderCount);
                            userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                                .set(UserOrderRecord::getOrderCount, orderCount)
                                .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
                            // 记录代理统计
                            AgentUserStatistics(divideMoney, sysUser, userInfo.getAppType(), order.getOrderType());
                        }
                    } else if ("1".equals(superUser.getAgentLevel())) {

                        // 查询平台的扣单比例，判断平台是否扣单
                        UserDeduction deduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                            .eq(UserDeduction::getDeductionType, "0"));

                        // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空
                        if (ObjectUtil.equals(orderCount, deduction.getOrderCount())) {
                            RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), orderCount);
                            userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                                .set(UserOrderRecord::getOrderCount, "0")
                                .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
                            // todo 记录扣单
                        } else {
                            // 平台不扣单，查询一级代理是否扣单
                            UserDeduction firstDeduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                                .eq(UserDeduction::getDeductionType, "1")
                                .eq(UserDeduction::getUserId, superUser.getUserId()));
                            // 一级代理扣单
                            if (ObjectUtil.equals(orderCount, firstDeduction.getOrderCount())) {
                                // 一级代理扣单
                                AgentUserStatistics(order.getOrderAmount(), superUser, userInfo.getAppType(), order.getOrderType());
                                superUser.setBalance(superUser.getBalance().add(order.getOrderAmount()));
                                sysUserMapper.updateById(superUser);
                                // todo 记录扣单
                            } else {
                                // 不满足扣单，按照分成比例进行分配
                                // 2级代理先分配。然后是一级代理得比例减去2级代理得比例才是一级得到得
                                BigDecimal commissionRatio = sysUser.getCommissionRatio();
                                BigDecimal divideMoney = commissionRatio.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                                sysUser.setBalance(sysUser.getBalance().add(divideMoney));
                                sysUserMapper.updateById(sysUser);
                                AgentUserStatistics(divideMoney, sysUser, userInfo.getAppType(), order.getOrderType());

                                BigDecimal commissionRatio_1 = superUser.getCommissionRatio().subtract(commissionRatio);
                                BigDecimal divideMoney_1 = commissionRatio_1.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                                superUser.setBalance(sysUser.getBalance().add(divideMoney_1));
                                sysUserMapper.updateById(superUser);
                                AgentUserStatistics(divideMoney_1, superUser, userInfo.getAppType(), order.getOrderType());

                            }
                        }
                    } else if ("2".equals(superUser.getAgentLevel())) {
                        // 查询2级代理的上级
                        SysUser twoSuperUser = sysUserMapper.selectVoOne(new LambdaUpdateWrapper<SysUser>()
                            .eq(SysUser::getSuperiorAgent, superUser.getUserId()));
                        // 如果二级代理的上级是平台
                        if ("0".equals(twoSuperUser.getAgentLevel())) {
                            // 查询平台的扣单比例，判断平台是否扣单
                            UserDeduction deduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                                .eq(UserDeduction::getDeductionType, "0"));

                            // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空
                            if (ObjectUtil.equals(orderCount, deduction.getOrderCount())) {
                                RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), orderCount);
                                userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                                    .set(UserOrderRecord::getOrderCount, "0")
                                    .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
                                // todo 记录扣单
                            } else {
                                // 如果平台不扣单，判断二级是否扣单
                                UserDeduction twoDeduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                                    .eq(UserDeduction::getDeductionType, "2"));
                                // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空
                                if (ObjectUtil.equals(orderCount, twoDeduction.getOrderCount())) {
                                    // 二级代理扣单
                                    AgentUserStatistics(order.getOrderAmount(), superUser, userInfo.getAppType(), order.getOrderType());
                                    superUser.setBalance(superUser.getBalance().add(order.getOrderAmount()));
                                    sysUserMapper.updateById(superUser);
                                    // todo 记录扣单
                                } else {
                                    // 二级代理不扣单
                                    // 3级代理先分配。然后是2级代理得比例减去3级代理得比例才是2级得到得
                                    BigDecimal commissionRatio = sysUser.getCommissionRatio();
                                    BigDecimal divideMoney = commissionRatio.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                                    sysUser.setBalance(sysUser.getBalance().add(divideMoney));
                                    sysUserMapper.updateById(sysUser);
                                    AgentUserStatistics(divideMoney, sysUser, userInfo.getAppType(), order.getOrderType());

                                    BigDecimal commissionRatio_1 = superUser.getCommissionRatio().subtract(commissionRatio);
                                    BigDecimal divideMoney_1 = commissionRatio_1.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                                    superUser.setBalance(sysUser.getBalance().add(divideMoney_1));
                                    sysUserMapper.updateById(superUser);
                                    AgentUserStatistics(divideMoney_1, superUser, userInfo.getAppType(), order.getOrderType());
                                }

                            }
                            // 二级代理的上级是一级代理
                        } else if ("1".equals(twoSuperUser.getAgentLevel())) {
                            // 查询平台的扣单比例，判断平台是否扣单
                            UserDeduction deduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                                .eq(UserDeduction::getDeductionType, "0"));

                            // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空
                            if (ObjectUtil.equals(orderCount, deduction.getOrderCount())) {
                                RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), orderCount);
                                userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                                    .set(UserOrderRecord::getOrderCount, "0")
                                    .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
                                // todo 记录扣单
                            } else {
                                // 如果平台不扣单，判断一级是否扣单
                                UserDeduction firstDeduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                                    .eq(UserDeduction::getDeductionType, "1"));
                                // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空
                                if (ObjectUtil.equals(orderCount, firstDeduction.getOrderCount())) {
                                    // 二级代理扣单
                                    AgentUserStatistics(order.getOrderAmount(), superUser, userInfo.getAppType(), order.getOrderType());
                                    superUser.setBalance(superUser.getBalance().add(order.getOrderAmount()));
                                    sysUserMapper.updateById(superUser);
                                    // todo 记录扣单
                                } else {
                                    // 如果一级不扣单，判断二级是否扣单
                                    UserDeduction twoDeduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
                                        .eq(UserDeduction::getDeductionType, "2"));
                                    // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空
                                    if (ObjectUtil.equals(orderCount, twoDeduction.getOrderCount())) {
                                        // 二级代理扣单
                                        AgentUserStatistics(order.getOrderAmount(), superUser, userInfo.getAppType(), order.getOrderType());
                                        superUser.setBalance(superUser.getBalance().add(order.getOrderAmount()));
                                        sysUserMapper.updateById(superUser);
                                        // todo 记录扣单
                                    } else {
                                        // 二级代理不扣单
                                        // 3级代理先分配。然后是2级代理得比例减去3级代理得比例才是2级得到得
                                        BigDecimal commissionRatio = sysUser.getCommissionRatio();
                                        BigDecimal divideMoney = commissionRatio.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                                        sysUser.setBalance(sysUser.getBalance().add(divideMoney));
                                        sysUserMapper.updateById(sysUser);
                                        AgentUserStatistics(divideMoney, sysUser, userInfo.getAppType(), order.getOrderType());
                                        // 2级分配
                                        BigDecimal commissionRatio_1 = superUser.getCommissionRatio().subtract(commissionRatio);
                                        BigDecimal divideMoney_1 = commissionRatio_1.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                                        superUser.setBalance(superUser.getBalance().add(divideMoney_1));
                                        sysUserMapper.updateById(superUser);
                                        AgentUserStatistics(divideMoney_1, superUser, userInfo.getAppType(), order.getOrderType());
                                        // 1级分配
                                        BigDecimal commissionRatio_2 = twoSuperUser.getCommissionRatio().subtract(commissionRatio);
                                        BigDecimal divideMoney_2 = commissionRatio_2.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
                                        superUser.setBalance(twoSuperUser.getBalance().add(divideMoney_2));
                                        sysUserMapper.updateById(twoSuperUser);
                                        AgentUserStatistics(divideMoney_1, twoSuperUser, userInfo.getAppType(), order.getOrderType());
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }

    }

    private void superPT(SysUser sysUser, UserInfo userInfo, MemberOrder order){
        Integer orderCount = getOrderCount(sysUser.getUserId()) + 1;

        // 查询平台的扣单比例，判断平台是否扣单
        UserDeduction deduction = deductionMapper.selectOne(new LambdaUpdateWrapper<UserDeduction>()
            .eq(UserDeduction::getDeductionType, "0"));

        // 订单数量满足平台扣单，此单扣除，更新代理订单数量表为空
        if (ObjectUtil.equals(orderCount, deduction.getOrderCount())) {
            // 更新订单数量
            RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), orderCount);
            userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                .set(UserOrderRecord::getOrderCount, "0")
                .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
        } else {
            // 不满足扣单，按照分成比例进行分配
            BigDecimal commissionRatio = sysUser.getCommissionRatio();
            BigDecimal divideMoney = commissionRatio.multiply(order.getOrderAmount()).setScale(2, RoundingMode.HALF_UP);
            sysUser.setBalance(sysUser.getBalance().add(divideMoney));
            sysUserMapper.updateById(sysUser);
            // 更新订单数量
            RedisUtils.setCacheObject(CacheConstants.USER_ORDER_KEY + sysUser.getUserId(), orderCount);
            userOrderRecordMapper.update(null, new LambdaUpdateWrapper<UserOrderRecord>()
                .set(UserOrderRecord::getOrderCount, orderCount)
                .eq(UserOrderRecord::getUserId, sysUser.getUserId()));
            // todo 记录扣单
            AgentUserStatistics(divideMoney, sysUser, userInfo.getAppType(), order.getOrderType());
        }
    }

    private Integer getOrderCount(Long userId) {
        Integer orderCount = 0;
        // 查询上级得订单数量
        String user_order_key = CacheConstants.USER_ORDER_KEY + userId;
        // 判断redis是否存在key
        boolean user_order_key_flag = RedisUtils.isExistsObject(user_order_key);
        if (user_order_key_flag) {
            orderCount = RedisUtils.getCacheObject(user_order_key);
        }

        if (!user_order_key_flag) {
            UserOrderRecord userOrderRecord = userOrderRecordMapper.selectOne(new LambdaUpdateWrapper<UserOrderRecord>()
                .eq(UserOrderRecord::getUserId, userId));
            orderCount = Integer.valueOf(userOrderRecord.getOrderCount());
        }
        return orderCount;
    }

    private void AgentUserStatistics(BigDecimal divideMoney, SysUser sysUser, String appType, String orderType){
        // 更新代理订单统计表
        AgentUserStatistics agentUserStatistics = agentUserStatisticsMapper.selectOne(new LambdaQueryWrapper<AgentUserStatistics>()
            .eq(AgentUserStatistics::getAgentId, sysUser.getUserId())
            .eq(AgentUserStatistics::getUserType, "sys_agent"));
        if (ObjectUtil.isNotNull(agentUserStatistics)) {
            // App类型（Android、IOS）
            if ("Android".equals(appType)){
                // 订单类型（0-充值钻石、1-开通会员）
                if ("0".equals(orderType)){
                    agentUserStatistics.setAndroidRechargeAmount(agentUserStatistics.getAndroidRechargeAmount().add(divideMoney));
                    agentUserStatistics.setRechargeAmount(agentUserStatistics.getRechargeAmount().add(divideMoney));
                    agentUserStatistics.setCommissionAmount(agentUserStatistics.getCommissionAmount().add(divideMoney));
                    agentUserStatisticsMapper.updateById(agentUserStatistics);
                } else {
                    agentUserStatistics.setMemberCount(agentUserStatistics.getMemberCount() + 1);
                    agentUserStatistics.setRechargeAmount(agentUserStatistics.getRechargeAmount().add(divideMoney));
                    agentUserStatistics.setCommissionAmount(agentUserStatistics.getCommissionAmount().add(divideMoney));
                    agentUserStatisticsMapper.updateById(agentUserStatistics);
                }

            } else {
                // 订单类型（0-充值钻石、1-开通会员）
                if ("1".equals(orderType)){
                    agentUserStatistics.setIosRechargeAmount(agentUserStatistics.getIosRechargeAmount().add(divideMoney));
                    agentUserStatistics.setRechargeAmount(agentUserStatistics.getRechargeAmount().add(divideMoney));
                    agentUserStatistics.setCommissionAmount(agentUserStatistics.getCommissionAmount().add(divideMoney));
                    agentUserStatisticsMapper.updateById(agentUserStatistics);
                } else {
                    agentUserStatistics.setMemberCount(agentUserStatistics.getMemberCount() + 1);
                    agentUserStatistics.setRechargeAmount(agentUserStatistics.getRechargeAmount().add(divideMoney));
                    agentUserStatistics.setCommissionAmount(agentUserStatistics.getCommissionAmount().add(divideMoney));
                    agentUserStatisticsMapper.updateById(agentUserStatistics);
                }
            }
        } else {
            // 第一次进统计表
            agentUserStatistics = new AgentUserStatistics();
            agentUserStatistics.setAgentId(sysUser.getUserId());
            agentUserStatistics.setUserType("sys_agent");
            agentUserStatistics.setAgentLevel(sysUser.getAgentLevel());
            agentUserStatistics.setCommissionRatio(sysUser.getCommissionRatio());
            agentUserStatistics.setRechargeAmount(divideMoney);
            agentUserStatistics.setCommissionAmount(divideMoney);
            if ("Android".equals(appType)){
                // 订单类型（0-充值钻石、1-开通会员）
                if ("0".equals(orderType)){
                    agentUserStatistics.setAndroidRechargeAmount(divideMoney);
                    agentUserStatisticsMapper.insert(agentUserStatistics);
                } else {
                    agentUserStatistics.setMemberCount("1");
                    agentUserStatisticsMapper.insert(agentUserStatistics);
                }

            } else {
                // 订单类型（0-充值钻石、1-开通会员）
                if ("1".equals(orderType)){
                    agentUserStatistics.setIosRechargeAmount(divideMoney);
                    agentUserStatisticsMapper.insert(agentUserStatistics);
                } else {
                    agentUserStatistics.setMemberCount("1");
                    agentUserStatisticsMapper.insert(agentUserStatistics);
                }
            }

        }
    }
}
